<template>
<!--  商品规格页面-->
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__good">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item prop="productId">
          <ComSelectPage ref="newPageSelect" @selectPageChange="e=>dataForm.productId=e"
                         :getUrl="{
                            url:'/api/product/page',
                            method:'get',
                            params:{page: 1,limit: 10,total: 0},
                          }"  clearable
                         placeholder="请选择商品"
                         :defaultValue="dataForm.productId" selectLabel="productName" selectLabelTwo="groupName" selectValue="id"></ComSelectPage>
        </el-form-item>
        <el-form-item>
          <el-button  v-if="$hasPermission('api:productSpecs:page')" @click="getDataList()">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('api:productSpecs:save')" type="primary" @click="addOrUpdateHandle()">
            新增
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('api:productSpecs:delete')" type="danger" @click="deleteHandle()">
            删除
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
          v-loading="dataListLoading"
          :data="dataList"
          stripe highlight-current-row
          @selection-change="dataListSelectionChangeHandle"
          @sort-change="dataListSortChangeHandle"
          style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="productId" label="商品ID" header-align="center" align="center" width="200">
          <template slot-scope="{}" slot="header">
            <!-- 表头名称 -->
            <span>商品信息</span>
            <el-tooltip class="item" effect="dark" placement="top">
              <i class="el-icon-question" style="font-size:16px;"></i>
              <!-- 悬停提示内容 -->
              <div slot="content">
                <div>点击下方名称查看详细信息</div>
              </div>
            </el-tooltip>
          </template>
          <template slot-scope="{row}">
            <div class="flex_columns" @click="showProductList(row['productDTO'])" v-if="row['productDTO']">
              <div class="flex_rows flex_center">
                <el-avatar size="large" :src="row['productDTO']['imgUrl']" v-if="row['productDTO']['imgUrl']"></el-avatar>
                <div class="flex_columns" style="margin-left: 10px;">
                  <div class="flex_rows" style="text-decoration: underline;cursor: pointer;">ID:{{row['productDTO']['id']}}</div>
                  <div class="flex_rows flex_center" style="text-decoration: underline;cursor: pointer;">
                    <div class="ellipsis1" style="max-width: 150px;">
                      {{row['productDTO']['productName']}}
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div v-if="!row.productDTO">{{row.productId}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="imgUrl" label="主图" header-align="center" align="center" width="">
          <template slot-scope="{row,$index}">
            <!-- 图片展示 -->
            <el-popover trigger="click" placement="top" popper-class="popper">
              <img v-if="row.imgUrl" :src="row.imgUrl">
              <el-image v-if="row.imgUrl" slot="reference" :src="row.imgUrl" style="width: 45px;height: 45px;">
                <div slot="error">
                  暂无图片
                </div>
              </el-image>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="bodyImg" label="外盒" header-align="center" align="center" width="">
          <template slot-scope="{row,$index}">
            <!-- 图片展示 -->
            <el-popover trigger="click" placement="top" popper-class="popper">
              <img v-if="row.bodyImg" :src="row.bodyImg">
              <el-image v-if="row.bodyImg" slot="reference" :src="row.bodyImg" style="width: 45px;height: 45px;">
                <div slot="error">
                  暂无图片
                </div>
              </el-image>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="packageImg" label="瓶身" header-align="center" align="center" width="">
          <template slot-scope="{row,$index}">
            <!-- 图片展示 -->
            <el-popover trigger="click" placement="top" popper-class="popper">
              <img v-if="row.packageImg" :src="row.packageImg">
              <el-image v-if="row.packageImg" slot="reference" :src="row.packageImg" style="width: 45px;height: 45px;">
                <div slot="error">
                  暂无图片
                </div>
              </el-image>
            </el-popover>
          </template>
        </el-table-column>
<!--        <el-table-column prop="specsName" label="商品规格" header-align="center" align="center" width="" show-overflow-tooltip></el-table-column>-->
        <el-table-column prop="specsOption" label="规格名称" header-align="center" align="center" width="" show-overflow-tooltip></el-table-column>
        <el-table-column prop="price" label="规格价格" header-align="center" align="center" width="" show-overflow-tooltip>
          <template slot-scope="{row}">
            <div>{{ formatNumber(row.price) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="marketPrice" label="划线价格" header-align="center" align="center" width="" show-overflow-tooltip>
          <template slot-scope="{row}">
            <div>{{formatNumber(row.price)}}</div>
          </template>
        </el-table-column>
<!--        <el-table-column prop="points" label="赠予积分" header-align="center" align="center" width="" show-overflow-tooltip>-->
<!--          <template slot-scope="{row}">-->
<!--            <div>{{formatNumber(row.points)}}</div>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column prop="discount" label="分润" align="center" min-width="150">
          <template slot-scope="{row}">
            <div class="flex_columns">
              <!--                  <span>分润类型:{{$getDictLabel("profitType", row.profitType)}}</span>-->
              <span>省代收益:{{ formatNumber(row.proProfit) }}</span>
              <span>市代收益:{{ formatNumber(row.cityProfit) }}</span>
              <span>区县收益:{{ formatNumber(row.areaProfit) }}</span>
              <span>直推收益:{{ formatNumber(row.firstProfit) }}</span>
              <span>间推收益:{{ formatNumber(row.secProfit) }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="stock" label="规格库存" header-align="center" align="center" width="" show-overflow-tooltip>
          <template slot-scope="{row}">
            <div>{{formatNumber(row.stock,true)}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建日期" header-align="center" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
          <template slot-scope="scope">
            <el-link v-if="$hasPermission('api:productSpecs:update')" type="primary"
                       @click="addOrUpdateHandle(scope.row.id)">修改
            </el-link>
            <el-link v-if="$hasPermission('api:productSpecs:delete')"
                     type="primary"
                       @click="deleteHandle(scope.row.id)">删除
            </el-link>
          </template>
        </el-table-column>

      </el-table>
      <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <!--查看商品信息组件-->
      <com-product-list :visible.sync="showProduct" :content="content"></com-product-list>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './goods-spec-add-or-update.vue'
import {formatNumber} from "@/utils";

export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/api/productSpecs/page',
        getDataListIsPage: true,
        createdIsNeed: false,            // 此页面是否在创建时，调用查询数据列表接口？
        deleteURL: '/api/productSpecs',
        deleteIsBatch: true
      },
      showProduct:false,                   //是否显示信息
      content:{},                          //信息内容
      showViewer: false,
      dataForm: {
        id: '',
        imgUrl: '',                         //针对该规格的图片链接（主图）
        bodyImg: '',                        //针对该规格的图片链接（外盒）
        packageImg: '',                     //针对该规格的图片链接（瓶身）
        price: '',                          //针对该规格选项的价格
        marketPrice: '',	                  //针对该规格选项的划线价格
        productId: '',                      //关联到所属商品的ID
        specsName: '',                    	//规格的名称，如颜色、尺寸等
        specsOption: '',                    //规格的可选项，如红色、蓝色、XL、XXL等
        stock: '',                          //针对该规格选项的库存
        points: '',                         //购买产品可获得的积分
        proProfit:'',                       //省代收益
        cityProfit: '',                     //市代收益
        areaProfit: '',                     //区县收益
        firstProfit: '',                    //直推收益
        secProfit: '',                      //间推收益
      },
      timeList:[]                           //时间戳数组
    }
  },
  watch: {
    '$route':'getPath'
  },
  components: {
    AddOrUpdate
  },
  created() {
  },
  mounted() {
    if (this.$route.query.productId){
      this.mixinViewModuleOptions.createdIsNeed = false
      this.dataForm.productId = this.timeList.includes(this.$route.query.time)?'': this.$route.query.productId
      this.getDataList()//查询数据
    }else {
      this.mixinViewModuleOptions.createdIsNeed = true
      this.getDataList()//查询数据
    }
    this.timeList.push(this.$route.query.time)
  },
  methods:{
    formatNumber,
    //监听路由变化
    getPath(){
      if (this.$route.query.productId){
        this.mixinViewModuleOptions.createdIsNeed = false
        this.dataForm.productId = this.timeList.includes(this.$route.query.time)?'': this.$route.query.productId
        this.getDataList()//查询数据
      }else {
        this.mixinViewModuleOptions.createdIsNeed = true
        this.dataForm.productId = ''
        this.getDataList()//查询数据
      }
      this.timeList.push(this.$route.query.time)
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.dataForm.id = id
        this.$refs.addOrUpdate.dataForm.productId = this.dataForm.productId
        this.$refs.addOrUpdate.init()
      })
    },
    //显示商品信息
    showProductList(content){
      this.showProduct=true;
      this.content= content;
    },
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-image__inner {
  //height: 50px;
}
img {
  height: 400px;
}
</style>
